package com.hemaapp.yjnh.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.CouponsRecordAdapter;
import com.hemaapp.yjnh.bean.CouponRecord;
import com.hemaapp.yjnh.bean.User;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;

/**
 * 粮票的消费记录
 */
public class FoodStampCostListActivity extends BaseActivity {

    @Bind(R.id.tv_hint)
    TextView tvHint;
    @Bind(R.id.tv_coupons_count)
    TextView tvCouponsCount;
    @Bind(R.id.listview)
    XtomListView listview;
    @Bind(R.id.refreshLoadmoreLayout)
    RefreshLoadmoreLayout refreshLoadmoreLayout;
    @Bind(R.id.tv_title)
    TextView tvTitle;

    private CouponsRecordAdapter adapter;
    private ArrayList<CouponRecord> records = new ArrayList<CouponRecord>();
    private User user;
    private int page = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_coupons_pay);
        ButterKnife.bind(this);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
        getCouponList();
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void getExras() {
    }

    @Override
    protected void setListener() {
        refreshLoadmoreLayout.setOnStartListener(new OnStartListener() {

            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
                page = 0;
                getCouponList();
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
                page++;
                getCouponList();
            }
        });
    }

    private void getCouponList() {
        if (user != null) {
            getNetWorker().foodStampCostList(user.getToken(), page + "");
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_COST_LIST:
                showProgressDialog("请稍候");
                break;
            default:
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_COST_LIST:
                cancelProgressDialog();
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_COST_LIST:
                String page = netTask.getParams().get("page");
                HemaPageArrayResult<CouponRecord> cResult = (HemaPageArrayResult<CouponRecord>) baseResult;
                String count = user.getCouponCost();
                if (count != null) {
                    tvCouponsCount.setText(uuUtils.formatAfterDot2(count));
                } else {
                    tvCouponsCount.setText("0");
                }
                if (cResult == null || cResult.getObjects() == null)
                    return;
                if ("0".equals(page)) {// 刷新
                    refreshLoadmoreLayout.refreshSuccess();
                    refreshLoadmoreLayout.setLoadmoreable(true);
                    records.clear();
                    records.addAll(cResult.getObjects());
                } else {// 更多
                    refreshLoadmoreLayout.loadmoreSuccess();
                    if (cResult.getObjects().size() > 0) {
                        records.addAll(cResult.getObjects());
                    } else {
                        refreshLoadmoreLayout.setLoadmoreable(false);
                        XtomToastUtil.showShortToast(mContext, "已经到最后啦");
                    }
                }
                refreshData(-1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_COST_LIST:
                showTextDialog(baseResult.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_COST_LIST:
                showTextDialog("获取数据失败!");
                refreshData(failedType);
                break;
            default:
                break;
        }
    }

    private void refreshData(int type) {
        if (adapter == null) {
            adapter = new CouponsRecordAdapter(mContext, records, 1);
            adapter.setEmptyString("没有消费过粮票");
            listview.setAdapter(adapter);
        } else {
            adapter.setEmptyString("没有消费过粮票");
            adapter.notifyDataSetChanged();
        }
        adapter.setFailtype(type);
    }


    @OnClick(R.id.title_left_btn)
    public void onClick() {
        finish();
    }

//    private void initPopuptWindow() {
//        final View view = getLayoutInflater().inflate(R.layout.pop_coupon_pay_record_menu, null);
//        TextView tv_all = (TextView) view.findViewById(R.id.tv_all);
//        TextView tv_pay = (TextView) view.findViewById(R.id.tv_pay);
//        TextView tv_zhuanzhang = (TextView) view.findViewById(R.id.tv_zhuanzhang);
//        tv_all.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                mPopupWindow.dismiss();
//                type = "5";
//                getCouponList();
//            }
//        });
//        tv_pay.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                mPopupWindow.dismiss();
//                type = "2";
//                getCouponList();
//            }
//        });
//        tv_zhuanzhang.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                mPopupWindow.dismiss();
//                type = "3";
//                getCouponList();
//            }
//        });
//        mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//        //设置SelectPicPopupWindow弹出窗体可点击
//        mPopupWindow.setFocusable(true);
//        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
//        mPopupWindow.setOnDismissListener(new OnDismissListener() {
//
//            @Override
//            public void onDismiss() {
//                title_arrow.setImageResource(R.drawable.arrow_down_white);
//            }
//        });
//        view.setOnTouchListener(new OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int height = view.findViewById(R.id.ll_content).getBottom();
//                int y = (int) event.getY();
//                if (y > height) {
//                    mPopupWindow.dismiss();
//                }
//                return true;
//            }
//        });
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.title_left_btn:
//                finish();
//                break;
//            case R.id.title_text:
//            case R.id.title_arrow:
//                title_arrow.setImageResource(R.drawable.arrow_up_white);
//                if (null == mPopupWindow) {
//                    initPopuptWindow();
//                    mPopupWindow.showAsDropDown(layout_title);
//                } else if (mPopupWindow.isShowing()) {
//                    mPopupWindow.dismiss();
//                } else {
//                    mPopupWindow.showAsDropDown(layout_title);
//                }
//                break;
//            default:
//                break;
//        }
//    }

}
